package io.dcloud.test4050

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.util.TypedValue
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.toColorInt
import com.example.nativedom2test.Engine
import kotlin.math.roundToInt

class ViewText4050Activity : AppCompatActivity() {

    private lateinit var contentContainer: LinearLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 主容器
        val mainLayout = LinearLayout(this)
        mainLayout.orientation = LinearLayout.VERTICAL

        // 按钮容器
        val buttonContainer = LinearLayout(this)
        buttonContainer.orientation = LinearLayout.HORIZONTAL

        // 添加视图按钮
        val addButton = Button(this)
        addButton.text = "添加 View"
        addButton.setOnClickListener {
            addGridView()
        }

        buttonContainer.addView(addButton)

        // 用于动态添加内容的容器
        contentContainer = LinearLayout(this)
        contentContainer.orientation = LinearLayout.VERTICAL

        mainLayout.addView(buttonContainer)
        mainLayout.addView(contentContainer)

        setContentView(mainLayout)
    }

    private fun addGridView() {
        val sTime = System.currentTimeMillis()

        // 对应根 <view>
        val rootContainer = LinearLayout(this)
        rootContainer.orientation = LinearLayout.VERTICAL
        val rowCount = 50
        val colCount = 40
        val marginInPx = 3
        val lineHeight = dp2px(5).roundToInt()
        val color = "#85d8b4".toColorInt()
        for (i in 0 until rowCount) {
            val rowView = LinearLayout(this)
            rowView.orientation = LinearLayout.HORIZONTAL
            for (j in 0 until colCount) {
                val cellView = LinearLayout(this)
                cellView.orientation = LinearLayout.VERTICAL
                // 处理 margin 属性
                val layoutParams = LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
                )
                layoutParams.setMargins(marginInPx, 0, 0, marginInPx)
                cellView.layoutParams = layoutParams
                cellView.setBackgroundColor(color)
                val textView = TextView(this)
                textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 5f)
                textView.text = "$j"
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                    textView.lineHeight = lineHeight
                }
                cellView.addView(textView)
                rowView.addView(cellView)
            }
            rootContainer.addView(rowView)
        }

        contentContainer.addView(rootContainer)
        Log.e(
            "test",
            "addGridView ${rowCount * colCount * 2 + rowCount + 1} views took: ${System.currentTimeMillis() - sTime} ms"
        )
        rootContainer.post {
            val totalMs = System.currentTimeMillis() - sTime
            Log.e(
                "test",
                "addGridView renderEnd-------- ${rowCount * colCount * 2 + rowCount + 1} views took: ${totalMs} ms"
            )
            Toast.makeText(this@ViewText4050Activity, "总耗时: ${totalMs} ms", Toast.LENGTH_LONG).show()
        }
    }

    fun dp2px(value: Number): Float {
        if (value.toFloat().isInfinite() || value.toFloat().isNaN())
            return value.toFloat()
        val finalPx = value.toFloat() * Engine.getScale()
        return if (finalPx > 0.005 && finalPx < 1) 1f else finalPx
    }
}